/*
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 * User: andy
 * Date: 30/07/2006
 * Time: 08:37
 */

using System;
using System.IO;

namespace uk.co.aspellclark.common.ipaq
{
    /// <summary>
    ///   <name>AsciiFile</name>
    ///   <namespace>uk.co.aspellclark.common</namespace>
    ///   <version>1.0</version>
    ///   <author>Andy Aspell-Clark</author>
    ///   <description>
    ///   </description>
    ///   <history>
    ///     <historyitem>17 Nov 2004  1.0 ARA  Initial Version.</historyitem>
    ///   </history>
    /// </summary>
    ///
    public class AsciiFile
    {
        /// <summary>
        ///
        /// </summary>
        protected string m_sFilepath;

        /// <summary>
        /// the name of the file to read
        /// </summary>
        protected string m_sFilename;

        /// <summary>
        ///
        /// </summary>
        protected Int64 m_lFileSize;

        /// <summary>
        ///
        /// </summary>
        protected Int64 m_lPosition;

        /// <summary>
        ///
        /// </summary>
        protected FileInfo m_fiFileInfo;

        /// <summary>
        ///
        /// </summary>
        protected FileStream m_fsFileStream;



        #region Properties

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public string Filename
        {
            get { return m_sFilename; }
            set
            {
                int indexOf = value.LastIndexOf(System.IO.Path.DirectorySeparatorChar);
                if (indexOf == -1)
                {
	                m_sFilename = value;
	                m_sFilepath = @".\";
                }
                else
                {
	                m_sFilename = value.Substring(indexOf+1);
	                m_sFilepath = value.Substring(0 , indexOf+1);
                }
                if (!File.Exists(m_sFilepath + m_sFilename))
                {
                	File.Create(m_sFilepath + m_sFilename);
                }
                m_fiFileInfo = new FileInfo(m_sFilepath + m_sFilename);
                m_lFileSize = m_fiFileInfo.Length;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public Int64 FileSize
        {
            get {return m_lFileSize; }
        }

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public Int64 CurrentPosition
        {
            get {return m_lPosition; }
        }

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public int NumberOfLines
        {
            get
            {
                int iLines = 0;


                return iLines;
            }
        }


        #endregion

    }//class()



    /// <summary>
    ///   <name>AsciiFileReader</name>
    ///   <namespace>uk.co.aspellclark.common</namespace>
    ///   <version>1.0</version>
    ///   <author>Andy Aspell-Clark</author>
    ///   <description>
    ///   </description>
    ///   <history>
    ///     <historyitem>17 Nov 2004  1.0 ARA  Initial Version.</historyitem>
    ///   </history>
    /// </summary>
    public class AsciiFileReader : AsciiFile
    {
        /// <summary>
        ///
        /// </summary>
        protected StreamReader m_srTextStream;

        /// <summary>
        /// reads the first line of the file
        /// </summary>
        /// <returns>a list of the fields in the first line of the file</returns>
        public string FirstLineFromFile
        {
            get
            {
                string sFileLine = null;

                closeFile();
                openFile();
                sFileLine = m_srTextStream.ReadLine();
                if (sFileLine != null)
                {
                    m_lPosition += sFileLine.Length + 2;
                }

                return sFileLine;
            }
        }

        /// <summary>
        /// reads the first line of the file
        /// </summary>
        /// <returns>a list of the fields in the first line of the file</returns>
        public string NextLineFromFile
        {
            get
            {
                string sFileLine = null;
                sFileLine = m_srTextStream.ReadLine();
                if (sFileLine != null)
                {
                    m_lPosition += sFileLine.Length + 2;
                }
                return sFileLine;
            }
        }


        /// <summary>
        /// reads in the next line from the file
        /// </summary>
        /// <returns></returns>
        public string Read()
        {
            return NextLineFromFile.Trim();
        }

        /// <summary>
        /// opens the file ready for reading
        /// </summary>
        public void openFile()
        {
            m_srTextStream = new StreamReader(m_sFilepath + m_sFilename);
        }


        /// <summary>
        ///
        /// </summary>
        public void closeFile()
        {
            m_srTextStream.Close();
        }
    }//class()


    /// <summary>
    ///   <name>AsciiFileWriter</name>
    ///   <namespace>uk.co.aspellclark.common</namespace>
    ///   <version>1.0</version>
    ///   <author>Andy Aspell-Clark</author>
    ///   <description>
    ///   </description>
    ///   <history>
    ///     <historyitem>17 Nov 2004  1.0 ARA  Initial Version.</historyitem>
    ///   </history>
    /// </summary>
    public class AsciiFileWriter : AsciiFile
    {
        /// <summary>
        ///
        /// </summary>
        protected StreamWriter m_swTextStream;


        /// <summary>
        /// writes a text line out to the file and appends a newline to the end of it
        /// </summary>
        /// <param name="psLine"></param>
        public void WriteLine (string psLine)
        {
            m_swTextStream.WriteLine(psLine);
        }
        /// <summary>
        /// writes a text line out to the file
        /// </summary>
        /// <param name="psLine"></param>
        public void Write (string psLine)
        {
            m_swTextStream.Write(psLine);
        }


        /// <summary>
        /// opens the file ready for reading
        /// </summary>
        public void openFile()
        {
            m_swTextStream = new StreamWriter(m_sFilepath + m_sFilename);
        }


        /// <summary>
        ///
        /// </summary>
        public void closeFile()
        {
            m_swTextStream.Close();
        }
    }
}//namespace()
